package com.marisfrolg.storage;

import java.lang.ref.WeakReference;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.marisfrolg.adapter.GridViewHomeAdapter;
import com.marisfrolg.entity.HomeItem;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.GridView;

public class HomeActivity extends ItemActivity {
	@ViewInject(R.id.homeGridView)
	private GridView homeGridView;
	public Handler changeHandler;
	public final static int ACTIVITY_CHECK=0; 
	public final static int ACTIVITY_PICKOUT=1; 
	public final static int ACTIVITY_MOVE=2; 
	public final static int ACTIVITY_UPSHELF=3;
	public final static int ACTIVITY_BACKBIN=4;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		ViewUtils.inject(this);
		changeHandler=new ChangeHandler(this);
		homeGridView.setAdapter(new GridViewHomeAdapter(this));
		GridViewHomeAdapter adapter=(GridViewHomeAdapter)homeGridView.getAdapter();
		adapter.addItem(new HomeItem(HomeActivity.ACTIVITY_PICKOUT, "拣配", "fa-external-link"));
		adapter.addItem(new HomeItem(HomeActivity.ACTIVITY_MOVE, "移仓", "fa-retweet"));
		adapter.addItem(new HomeItem(HomeActivity.ACTIVITY_UPSHELF, "归仓(旧)", "fa-arrow-circle-up"));
		adapter.addItem(new HomeItem(HomeActivity.ACTIVITY_BACKBIN, "归仓(新)", "fa-sign-in"));
		adapter.addItem(new HomeItem(HomeActivity.ACTIVITY_CHECK, "盘点", "fa-stack-overflow"));
//		PackageManager manager = this.getPackageManager();
//		try {
//		        PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
//		        String appVersion = info.versionName; // 版本名
//		        int appCode = info.versionCode; // 版本号
//		} catch (NameNotFoundException e) {
//		        e.printStackTrace();
//		}
	}
	
	private static class ChangeHandler extends Handler{
		private final WeakReference<HomeActivity> wActivity;
		public ChangeHandler(HomeActivity wActivity){
			this.wActivity=new  WeakReference<HomeActivity>(wActivity);
		}
		@Override
		public void handleMessage(Message msg) {
			HomeActivity activity=wActivity.get();
			Intent in=new Intent();
			switch (msg.what) {
			case ACTIVITY_CHECK:
				in.setClass(activity, CheckActivity.class);
				break;
			case ACTIVITY_PICKOUT:
				in.setClass(activity, PickoutActivity.class);
				break;
			case ACTIVITY_MOVE:
				in.setClass(activity, MoveActivity.class);
				break;
			case ACTIVITY_UPSHELF:
				in.setClass(activity, UpshelfActivity.class);
				break;
			case ACTIVITY_BACKBIN:
				in.setClass(activity, BackbinActivity.class);
				break;
			default:
				break;
			}
			activity.startActivity(in);
		}
		
	}
	@Override
	public void onBackPressed() {
		android.os.Process.killProcess(android.os.Process.myPid());
	}
}
